package com.alks.function.controller.web;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.WebLogger;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.DateUtils;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.common.utils.stringUtils.ZStringUtils;
import com.alks.entity.data.entity.ExportPlanInfo;
import com.alks.entity.data.entity.SdCompany;
import com.alks.entity.data.entity.webPay.FinanceReceivable;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.mapper.web.pay.PcPayMapper;
import com.alks.function.service.web.pay.PcPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:hxl
 * @create: 2024-11-22 10:04
 * @Description:
 */
@ApiRestController("/pc/pay")
@Api(tags = {"财务应收作业"})
public class PcPayController {
    @Autowired
    PcPayService pcPayService;

    @PostMapping("/receivable/list")
    @ApiOperation(value = "应收应付列表", tags = "财务应收作业")
    @WebLogger
    public ResponseInfo receivableList(@RequestBody FinanceReceivable req) {
        return pcPayService.receivableList(req);
    }

    @PostMapping({"/detail/list"})
    @ApiOperation(value = "应收应付明细", tags = "财务应收作业")
    @WebLogger
    public ResponseInfo selectArDetailList(@RequestBody FinanceReceivable req) {
        return pcPayService.selectArDetailList(req);
    }

    @PostMapping({"/addDetail"})
    @ApiOperation(value = "新增应收应付明细", tags = "财务应收作业")
    @WebLogger
    public ResponseInfo addDetail(@RequestBody List<FinanceReceivable> req) {
        return pcPayService.addDetail(req);
    }

    @PostMapping({"/deleteDetail"})
    @ApiOperation(value = "删除应收应付明细", tags = "财务应收作业")
    @WebLogger
    public ResponseInfo deleteDetail(@RequestBody FinanceReceivable req) {
        return pcPayService.deleteDetail(req);
    }

    @PostMapping({"/infoList"})
    @ApiOperation(value = "应收", tags = "财务应收作业")
    @WebLogger
    public ResponseInfo infoList(@RequestBody ExportPlanInfo exportPlanInfo) {
        return pcPayService.infoList(exportPlanInfo);
    }

    @PostMapping({"/cfm"})
    @ApiOperation(value = "确认", tags = "财务应收作业")
    @WebLogger
    public ResponseInfo cfm(@RequestParam String receiptNo, @RequestParam String cfmFlag) {
        return pcPayService.cfm(receiptNo, cfmFlag);
    }

    @PostMapping({"/updateRemark"})
    @ApiOperation(value = "修改备注", tags = "财务应收作业")
    @WebLogger
    public ResponseInfo updateRemark(@RequestParam String receiptNo, @RequestParam String remark) {
        return pcPayService.updateRemark(receiptNo, remark);
    }

    @PostMapping({"/tempDetail/list"})
    @ApiOperation(value = "临时应收应付明细", tags = "财务应收作业")
    @WebLogger
    public ResponseInfo tempDetailList(@RequestBody FinanceReceivable req) {
        return pcPayService.tempDetailList(req);
    }

    @PostMapping({"/addTempDetail"})
    @ApiOperation(value = "新增临时应收应付明细", tags = "财务应收作业")
    @WebLogger
    public ResponseInfo addTempDetail(@RequestBody List<FinanceReceivable> req) {
        return pcPayService.addTempDetail(req);
    }

    @PostMapping({"/tempDetail/deleteList"})
    @ApiOperation(value = "查询删除临时应收应付明细", tags = "财务应收作业")
    @WebLogger
    public ResponseInfo selectTempDeleteList(@RequestBody FinanceReceivable req) {
        return pcPayService.selectTempDeleteList(req);
    }

    @PostMapping({"/deleteTempDetail"})
    @ApiOperation(value = "删除缓存的财务应收明细", tags = "财务应收作业")
    @WebLogger
    public ResponseInfo deleteTempDetail(@RequestBody FinanceReceivable req) {

        List<FinanceReceivable> result = (List<FinanceReceivable>) pcPayService.selectTempDeleteList(req).getData();
        int deleteRow = pcPayService.deleteTempByReceiptNo(req);
        if (deleteRow > 0) {
            return ResponseInfo.ok(result, "还原成功");
        } else {
            return ResponseInfo.error("还原失败");
        }
    }

    @PostMapping({"/print"})
    @ApiOperation(value = "打印", tags = "财务应收作业")
    @WebLogger
    public ResponseInfo print(@RequestBody FinanceReceivable financeReceivable) {
        Map<String, Object> context = new HashMap<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SdCompany companyInfo = pcPayService.getCompanyById("HF");
        context.put("title", companyInfo.getChiName());
        context.put("printDate", format.format(new Date()));
        context.put("custom", financeReceivable.getCustom());
        context.put("recDate", format.format(financeReceivable.getRecDate()));
        context.put("receiptNo", financeReceivable.getReceiptNo());
        List<FinanceReceivable> list = pcPayService.selectPrintDataByPayNo(financeReceivable);
        context.put("detailList", list);
        BigDecimal totalAmt = BigDecimal.valueOf(0L);
        String totalRemark = "";
        for (FinanceReceivable item : list) {
            totalAmt = totalAmt.add(BigDecimal.valueOf(item.getRecAmt().doubleValue()));
            if (ZStringUtils.isNotEmpty(item.getRemark())) {
                totalRemark = item.getRemark();
            }
        }
        context.put("tatalNum", Integer.valueOf(list.size()));
        context.put("totalAmt", totalAmt);
        context.put("upperAmt", ZStringUtils.convert(totalAmt.doubleValue()));
        context.put("sysUser", financeReceivable.getSysUser());
        context.put("totalRemark", totalRemark);
        return ResponseInfo.ok(context);
    }

    @Autowired
    PcPayMapper pcPayMapper;
    @PostMapping({"/export"})
    @ApiOperation(value = "应收作业导出", tags = "财务应收作业")
    @WebLogger
    public void export(@RequestBody FinanceReceivable financeReceivable, HttpServletResponse response) throws IOException {
        if (financeReceivable.getActAmt() == null) {
            financeReceivable.setActAmt(Double.valueOf(2.0D));
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        financeReceivable.setCompanyId(UserIdThread.get().getCompanyId());
        List<FinanceReceivable> list = pcPayMapper.receivableList(financeReceivable);
        for (FinanceReceivable receivable : list) {
            String string = "Y".equals(receivable.getCfmFlag()) ? "是" : "否";
            receivable.setCfmFlag(string);
            if (receivable.getDeliveryDate()!=null) receivable.setDeliveryTime(format.format(receivable.getDeliveryDate()));
            if (receivable.getShipDate()!=null)receivable.setShipTime(format.format(receivable.getShipDate()));
            if (receivable.getOutDate()!=null)receivable.setOutTime(format.format(receivable.getOutDate()));
            if (receivable.getCfmTime()!=null)receivable.setCfmDate(format.format(receivable.getCfmTime()));
        }
        ExcelUtils.creatByTemplate(response, "应收作业导出模板.xls", "Receivable_"+ DateUtils.dateTimeNow(), list);
    }
}
